<?php
include('common/constants.php');
include('common/db.php');

if (!isset( $_POST )){
	echo "Nothing to save.";
}
//echo var_dump($_POST);

//create array to temporarily grab variables
$input_arr = array();
//grabs the $_POST variables and adds slashes
foreach ($_POST as $key => $input_arr) {
	$_POST[$key] = addslashes(htmlspecialchars($input_arr));
}

//$travelRoute = str_replace("&quot;", '"', $_POST['route_string']);
//echo 'TEST Route : '. $travelRoute ;



$sql = "INSERT INTO `advertisement`(`advert_id`, `email`, `ref_number`, `advertisement_type`, `origin`, `destination`, `travel_distance`, `travel_time`, `price`, `description`, `origin_time`, `dest_time`, `travel_date`, `vehicle_type_id`, `route_name`, `split_cost`, `take_turns`, `daily`, `weekly`, `monthly`, `sun`, `mon`, `tue`, `wed`, `thu`, `fri`, `sat`, `travel_route`) ";
//$sql .= " VALUES('{$_POST['advert_id']}', '{$_POST['email']}', '{$_POST['ref_number']}', '{$_POST['advertisement_type']}', '{$_POST['origin']}', '{$_POST['destination']}', '{$_POST['description']}', DATE_FORMAT('{$_POST['origin_time']}', '%h'), DATE_FORMAT('{$_POST['dest_time']}', '%h'), '{$_POST['travel_date']}', '{$_POST['route_name']}', '{$_POST['split_cost']}', '{$_POST['take_turns']}', '{$_POST['daily']}', '{$_POST['weekly']}', '{$_POST['monthly']}', '{$_POST['sun']}', '{$_POST['mon']}', '{$_POST['tue']}', '{$_POST['wed']}', '{$_POST['thu']}', '{$_POST['fri']}', '{$_POST['sat']}');";
$sql .= " VALUES('{$_POST['advert_id']}', '{$_POST['email']}', '{$_POST['ref_number']}', '{$_POST['advertisement_type']}', '{$_POST['origin']}', '{$_POST['destination']}', '{$_POST['travel_distance']}', '{$_POST['travel_duration']}', '{$_POST['price_per_seat']}', '{$_POST['description']}', '" . date("H:i", strtotime($_POST['origin_time'])) . "', '" . date("H:i", strtotime($_POST['dest_time'])) . "', '{$_POST['travel_date']}', '{$_POST['vehicle_type_id']}', '{$_POST['route_name']}', '{$_POST['split_cost']}', '{$_POST['take_turns']}', '{$_POST['daily']}', '{$_POST['weekly']}', '{$_POST['monthly']}', '{$_POST['sun']}', '{$_POST['mon']}', '{$_POST['tue']}', '{$_POST['wed']}', '{$_POST['thu']}', '{$_POST['fri']}', '{$_POST['sat']}', '{$_POST['route_string']}');";

//echo $sql;

if(mysql_query($sql)){

	$advert_no = mysql_insert_id();

	for ($i=1; $i <= $_POST['numOfSeats']; $i++) { 
		$sql2 = " INSERT INTO `seat`(`vehicle_type_id`, `advert_id`, `seat_status`) ";
		$sql2 .= " VALUES('{$_POST['vehicle_type_id']}', {$advert_no}, 'open'); ";
		mysql_query($sql2);
	}
	echo "success";
}else{
	echo "fail";
}